<?php
/**
 * 后台广告管理模块 Model
 * ============================================================================
 * TBlog TBlog博客系统　BY Tmac PHP MVC framework
 * $Author: zwt007 $  <zwttmac@qq.com>
 * $Id: ad.class.php 31 2011-07-11 15:25:51Z zwt007 $
 * http://www.t-mac.org；
 */
class ad extends Model
{

    private $ad_table;

    /**
     * 初始化变量　定义私有变量
     */
    public function _init()
    {
        //连接数据库
        $this->connect();
        $this->ad_table = '#@__ad';
    }

    /**
     * 保存
     * @param string $info
     * return Boole
     */
    public function addAd($info, $mode = 'INSERT', $where = '')
    {
        $rs = $this->db->autoExecute($this->ad_table, $info, $mode, $where);
        return $rs;
    }

    /**
     * 获取一个广告的详细信息
     * @param int $ad_id 栏目id
     * return array
     */
    public function getAdInfo($id)
    {
        $rs = $this->db->getRow("SELECT * FROM $this->ad_table WHERE ad_id = $id LIMIT 0,1");
        return $rs;
    }

    /**
     * 获取所有广告
     * return article_class,pages
     */
    public function getAdList($url = null)
    {
        $count = $this->db->getOne("SELECT COUNT(*) FROM $this->ad_table");
        if ($url == '')
            $url = PHP_SELF . '?m=ad&page=';
        $pages = $this->P('Pages');
        $pages->setTotal($count);
        $pages->setUrl($url);
        $pages->setPrepage(10);
        $limit = $pages->getSqlLimit();
        $rs = $this->db->getAll("SELECT ad_id, ad_area, ad_title, ad_link, ad_state_radio, ad_type_radio, ad_addtime FROM $this->ad_table ORDER BY ad_id DESC LIMIT $limit");
        //广告状态数组
        $ad_state_radio_ary = UtilityConfig::ad_state_radio_ary();
        //广告类型数组
        $ad_type_radio_ary = UtilityConfig::ad_type_radio_ary();
        foreach ($rs AS $k => $v) {
            $rs[$k]['state_name'] = $ad_state_radio_ary[$v['ad_state_radio']];
            $rs[$k]['type_name'] = $ad_type_radio_ary[$v['ad_type_radio']];
            $rs[$k]['time'] = date('Y-m-d H:i:s', $v['ad_addtime']);
        }

        //把文章的当前page写到cookies里
        HttpResponse::setCookie('ad_page', $pages->getNowPage());

        $ErrorMsg = '';
        if ($count == 0) {
            $ErrorMsg = "暂无资讯文章!   <a href='{PHP_SELF}?m=ad.add' class='link_a'>点我添加广告</a>";
        }
        
        $this->assign('rs', $rs);
        $this->assign('pageCurrent', $pages->getNowPage());
        $this->assign('page', $pages->show());
        $this->assign('ErrorMsg', $ErrorMsg);
        return $rs;
    }

}